package com.noodles.mall.service.impl;

import com.noodles.mall.entity.ItemType;
import com.noodles.mall.mapper.ItemTypeMapper;
import com.noodles.mall.service.ItemTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/** 商品类别 **/
@Service
public class ItemTypeServiceImpl extends ServiceImpl<ItemTypeMapper, ItemType> implements ItemTypeService, InitializingBean {

    @Override
    public void afterPropertiesSet(){
        STRING_ITEM_TYPE_MAP.clear();
        INTEGER_ITEM_TYPE_MAP.clear();
        super.list().forEach(itemType -> {
            STRING_ITEM_TYPE_MAP.put(itemType.getName(),itemType);
            INTEGER_ITEM_TYPE_MAP.put(itemType.getId(),itemType);
        });
    }

    /** 新增 **/
    public boolean insert(ItemType itemType){
        boolean f=super.save(itemType);
        if(f) afterPropertiesSet();
        return f;
    }

    /** 修改:  **/
    public boolean update(ItemType itemType){
        if(super.updateById(itemType)) {
            afterPropertiesSet();
            return true;
        }
        return false;
    }

    /** 单个查询: id **/
    @Override
    public ItemType selectOne(Integer id) {
        return INTEGER_ITEM_TYPE_MAP.get(id);
    }

    /** 单个查询: name **/
    public ItemType selectOne(String name){
        return STRING_ITEM_TYPE_MAP.get(name);
    }

    /** 列表查询:  **/
    public List<ItemType> selectList(){
        ArrayList<ItemType> list=new ArrayList<>();
        INTEGER_ITEM_TYPE_MAP.values().forEach(itemType -> list.add(itemType));
        return list;
    }


}
